package wizard;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 
 * @author michau
 *
 *	Class responsible for reacting to the 3 buttons actions from Wizard
 *	the controller will update the model to show a new panel
 *	and handle the new state of buttons
 */
public class WizardController implements ActionListener{
	private Wizard wizard;
	
	/**
	 * Uses w to get the WizardModel and update buttons.
	 * @param w callback to the wizard that created this controller
	 */
	public WizardController(Wizard w){
		wizard = w;
	}
	
	// handle actions performed by wizard's buttons
	private void cancelButtonPressed(){
		wizard.close(Wizard.CANCEL_RETURN_CODE);
	}
	
	private void nextButtonPressed(){
		System.out.println("WizardController: next button pressed.");
		WizardModel model = wizard.getModel();
		WizardPanelDescriptor descriptor = model.getCurrentPanelDescriptor();
		
		// if this is the last panel, close the dialog. Otherwise get the next panel's id and show this panel
		Object nextPanelDescriptor = descriptor.getNextPanelDescriptor();
		
		if (nextPanelDescriptor == null || 
				nextPanelDescriptor.equals(WizardPanelDescriptor.FINISH_IDENTIFIER)){
			wizard.close(Wizard.FINISH_RETURN_CODE);
		}else{
			wizard.setCurrentPanel(nextPanelDescriptor);
		}
	}
	
	private void backButtonPressed(){
		WizardModel model = wizard.getModel();
		WizardPanelDescriptor descriptor = model.getCurrentPanelDescriptor();
		
		Object backPanelDescriptor = descriptor.getBackPanelDescriptor();
		wizard.setCurrentPanel(backPanelDescriptor);
	}
	
    void resetButtonsToPanelRules() {
        
        //  Reset the buttons to support the original panel rules,
        //  including whether the next or back buttons are enabled or
        //  disabled, or if the panel is finishable.
        
        WizardModel model = wizard.getModel();
        WizardPanelDescriptor descriptor = model.getCurrentPanelDescriptor();
        
        model.setCancelButtonText(Wizard.CANCEL_TEXT);
        model.setCancelButtonIcon(Wizard.CANCEL_ICON);
        
        //  If the panel in question has another panel behind it, enable
        //  the back button. Otherwise, disable it.
        
        model.setBackButtonText(Wizard.BACK_TEXT);
        model.setBackButtonIcon(Wizard.BACK_ICON);
        
        if (descriptor.getBackPanelDescriptor() != null)
            model.setBackButtonEnabled(Boolean.TRUE);
        else
            model.setBackButtonEnabled(Boolean.FALSE);

        //  If the panel in question has one or more panels in front of it,
        //  enable the next button. Otherwise, disable it.
 
        if (descriptor.getNextPanelDescriptor() != null)
            model.setNextFinishButtonEnabled(Boolean.TRUE);
        else
            model.setNextFinishButtonEnabled(Boolean.FALSE);
 
        //  If the panel in question is the last panel in the series, change
        //  the Next button to Finish. Otherwise, set the text back to Next.
        
        if (descriptor.getNextPanelDescriptor() == null ||
        		descriptor.getNextPanelDescriptor().equals(WizardPanelDescriptor.FINISH_IDENTIFIER)) {
            model.setNextFinishButtonText(Wizard.FINISH_TEXT);
            model.setNextFinishButtonIcon(Wizard.FINISH_ICON);
        } else {
            model.setNextFinishButtonText(Wizard.NEXT_TEXT);
            model.setNextFinishButtonIcon(Wizard.NEXT_ICON);
        }
        
    }

	/**
	 * {@link ActionListener}
	 * Listens for actions performed by wizard's buttons
	 * @param e
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals(Wizard.CANCEL_BUTTON_ACTION_CMD))
            cancelButtonPressed();
        else if (e.getActionCommand().equals(Wizard.BACK_BUTTON_ACTION_CMD))
            backButtonPressed();
        else if (e.getActionCommand().equals(Wizard.NEXT_BUTTON_ACTION_CMD))
            nextButtonPressed();		
	}
}
